package net.jgrm.product.dto;

import lombok.Data;

import java.io.Serializable;

/**
 * Dubbo服务调用结果统一响应DTO
 */
@Data
public class DubboServiceResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 响应码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public DubboServiceResult() {
    }

    public DubboServiceResult(boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功响应
     */
    public static <T> DubboServiceResult<T> success(T data) {
        return new DubboServiceResult<>(true, "200", "操作成功", data);
    }

    /**
     * 成功响应（无数据）
     */
    public static <T> DubboServiceResult<T> success() {
        return new DubboServiceResult<>(true, "200", "操作成功", null);
    }

    /**
     * 失败响应
     */
    public static <T> DubboServiceResult<T> failure(String message) {
        return new DubboServiceResult<>(false, "500", message, null);
    }

    /**
     * 失败响应（带错误码）
     */
    public static <T> DubboServiceResult<T> failure(String code, String message) {
        return new DubboServiceResult<>(false, code, message, null);
    }
}